package com.example.asus.keshe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class GridViewActivity extends Activity implements AdapterView.OnItemClickListener{

    private GridView gridView;

    //定义图标的ID的数组
    private int[] icon = {R.drawable.game,R.drawable.weather};

    //定义图标对应的名字的数组
    private String[] iconName = {"记录","数据库内容"};

    private List<Map<String,Object>> data;

    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_demo);

        gridView = (GridView)findViewById(R.id.gridview);
        init();
        simpleAdapter = new SimpleAdapter(this,data,R.layout.gridview_item,
                new String[]{"image","title"},new int[]{R.id.imageView3,R.id.textView4});
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(this);
    }

    /**
     * 初始化菜单，把每一个菜单项对应的图标和标题生成一个hashmap
     */
    public void init(){
        data = new ArrayList<Map<String,Object>>();
        for(int i=0;i<iconName.length;i++){
            Map<String,Object> hm = new HashMap<String,Object>();
            hm.put("image",icon[i]);
            hm.put("title",iconName[i]);
            data.add(hm);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        switch (position){
            case 0://游戏

                intent.setClass(GridViewActivity.this,AddDemoActivity.class);
                startActivity(intent);
                break;

            case 1://数据库
                intent.setClass(this,SqlLiteDemoActivity.class);
                startActivity(intent);
                break;

        }
    }
}
